package com.sxfq.loansupermarket.backsystem.modular.analysis.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.analysis.dao.CountTwoDao;
import com.sxfq.loansupermarket.backsystem.modular.analysis.entity.PageHomeCount;
import com.sxfq.loansupermarket.backsystem.modular.analysis.entity.PlatFormCount;
import com.sxfq.loansupermarket.backsystem.modular.analysis.entity.RegisterCount;
import com.sxfq.loansupermarket.backsystem.modular.analysis.service.ICountServiceTwo;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;

/**
 * @author gaofucheng:
 * @version 2017年8月24日 上午11:09:22 类说明
 */
@Service
public class CountServiceTwoImpl implements ICountServiceTwo {

	@Autowired
	private SqlMapper sqlMapper;

	@Autowired
	private CountTwoDao countDao;

	/*
	 * 数据统计页面的数据列表 即app首页面统计所需要的数据集合
	 */
	@Override
	public List<PageHomeCount> selectHomePageCount(String startTime, String endTime) {
		StringBuffer sql = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sql.append(
				"select home_page_id,button_id,banner_id,star_platform_id,card_page_id,sum(visit_count) as summ from customer_platform_count_info where 1=1");
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			sql.append(" and update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + endTime
					+ ", 23:59:59') ");
		}
		/*if(StringUtil.isEmpty(startTime) && StringUtil.isEmpty(endTime)){
			Date now = new Date();
			startTime = sdf.format(now);
			sql.append(" and update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + startTime
					+ ", 23:59:59') ");
		}*/

		sql.append(" group by home_page_id,button_id,banner_id,star_platform_id,card_page_id");
		List<PageHomeCount> buttonList = sqlMapper.selectList(sql.toString(), PageHomeCount.class);
		return buttonList;
	}

	/*
	 * 数据统计页面的数据列表 即app首页面统计登录用户所需要的数据集合
	 */
	@Override
	public List<PageHomeCount> selectHomePageLoginCount(String startTime, String endTime) {
		StringBuffer sql = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sql.append(
				"select home_page_id,button_id,banner_id,star_platform_id,card_page_id,sum(login_visit_count) as summ from customer_platform_count_info where 1=1");
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			sql.append(" and login_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + endTime
					+ ", 23:59:59') ");
		}
		/*if(StringUtil.isEmpty(startTime) && StringUtil.isEmpty(endTime)){
			Date now = new Date();
			startTime = sdf.format(now);
			sql.append(" and update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + startTime
					+ ", 23:59:59') ");
		}*/
		sql.append(" group by home_page_id,button_id,banner_id,star_platform_id,card_page_id");
		List<PageHomeCount> buttonList = sqlMapper.selectList(sql.toString(), PageHomeCount.class);
		return buttonList;
	}

	/*
	 * 贷款总浏览量
	 */
	@Override
	public Integer getTotalLoanCount(String startTime, String endTime) {
		String sql = " select platform_id as abbreviation,sum(visit_count) as visitCount from customer_platform_count_info where 1=1";
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			sql += " and update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + endTime
					+ ", 23:59:59') ";
		}
		sql += " group by platform_id ";
		List<PlatFormCount> platformList = sqlMapper.selectList(sql, PlatFormCount.class);
		Integer totalPlatform = 0;
		for (PlatFormCount pfc : platformList) {
			if (StringUtil.isNotEmpty(pfc)) {
				String name = pfc.getAbbreviation();
				if (StringUtil.isNotEmpty(name)) {
					if (pfc.getVisitCount() != null) {
						totalPlatform += pfc.getVisitCount();
					} else {
						totalPlatform += 0;
					}
				}
			}
		}
		return totalPlatform;
	}

	/*
	 * 产品统计的数据列表
	 */
	@Override
	public List<PlatFormCount> selectPlatformCount(String startTime, String endTime) {
		StringBuffer sql = new StringBuffer();
		sql.append(
				"select sum(visit_count) as visitCount,sum(click_count) as clickCount,pp.product_name as abbreviation from customer_platform_count_info cp left join "
						+ "platform_product_info pp on cp.platform_id= pp.id where 1=1");
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			sql.append(" and cp.update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + endTime
					+ ", 23:59:59') ");
		}
		sql.append(" group by cp.platform_id");
		List<PlatFormCount> pfcList = sqlMapper.selectList(sql.toString(), PlatFormCount.class);
		return pfcList;
	}

	/*
	 * 页码的产品统计数据列表
	 */
	@Override
	public List<PlatFormCount> selectPageListByQueryGetMap(Page<PlatFormCount> page, String beginTime, String endTime) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", page);
		if (StringUtil.isNotEmpty(beginTime)) {
			params.put("beginTime", beginTime);
		}
		if (StringUtil.isNotEmpty(endTime)) {
			params.put("endTime", endTime);
		}
		return countDao.getQueryPage(page, params);

	}

	/*
	 * 注册总数
	 */
	@Override
	public Integer getTotalRegister(String startTime, String endTime) {
		String sql = "select count(1) from register_table where 1=1";
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			sql += " and update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + endTime
					+ ", 23:59:59') ";
		}
		Integer totalRegister = sqlMapper.selectOne(sql, Integer.class);
		return totalRegister;
	}

	/*
	 * 注册统计的数据列表
	 */
	@Override
	public List<RegisterCount> selectRegisterCount(String startTime, String endTime) {
		StringBuffer sql = new StringBuffer();
		sql.append(
				"select count(1) as registerCount, channel_name,click_count from register_table rt left join cms_channel cc on cc.id= rt.channel_id "
						+ "left join customer_platform_count_info cp on cp.channel_id = cc.id where 1=1");
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			sql.append(" and cp.update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + endTime
					+ ", 23:59:59') ");
		}
		sql.append(" group by channel_id");
		List<RegisterCount> registerList = sqlMapper.selectList(sql.toString(), RegisterCount.class);
		return registerList;
	}

	/*
	 * 获取首页banner个数
	 */
	@Override
	public Integer getBannerNum() {
		String sql = "select count(1) from cms_content where channel_id=2 and status=1";
		Integer bannerNum = sqlMapper.selectOne(sql, Integer.class);
		return bannerNum;
	}

}
